package com.fstech.whiteboadrd.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.EditText;

import com.fstech.whiteboadrd.R;
import com.fstech.whiteboadrd.utils.UIUtils;

@SuppressLint("AppCompatCustomView")
public class LineaEditText extends EditText {

    public LineaEditText(Context context) {
        super(context);
        init();
    }

    public LineaEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LineaEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public LineaEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }


    // 画笔 用来画下划线
    private Paint paint;

    public void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
//        paint.setColor(Color.RED);
        // 开启抗锯齿 较耗内存
        paint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 得到总行数
        int lineCount = getLineCount();
        // 得到每行的高度
        int lineHeight = getLineHeight();
        // 根据行数循环画线
        for (int i = 0; i < lineCount; i++) {
            int lineY = (i + 1) * lineHeight;
            if (isFocused()) {
                paint.setColor(UIUtils.getColor(R.color.edit_active));
            } else {
                paint.setColor(UIUtils.getColor(R.color.edit_normal));
            }
            canvas.drawLine(0, getHeight(), this.getWidth(), getHeight(), paint);
        }

    }

}
